﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Entity;
using zichan.Models;

namespace zichan.Controllers
{
    public class RoleController : BaseController
    {

        // GET: Admin/Role
        public ActionResult Index()
        {
            ViewBag.Menus = MenuService.GetMenuAll().Where(o => o.Level > 0).OrderBy(o => o.Path).ToDictionary(o => o.Id, j => j.Title);
            return View(RoleService.List());
        }

        public ActionResult Create()
        {
            ViewBag.Menus = MenuService.GetMenuAll();
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Exclude = "Id")] RoleModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Menus = MenuService.GetMenuAll().Where(o => o.Level > 0).OrderBy(o => o.Path).ToDictionary(o => o.Id, j => j.Title);
                return View(model);
            }
            if(model.Powers==null)
            {
                TempData["alert"] = "error#请选择所要添加的权限信息";
                return RedirectToAction("index");
            }
            Role role = new Role { Name = model.Name, Id = model.Id, Powers = string.Join(",", model.Powers) };
            string err = RoleService.RoleAdd(role);
            if (string.IsNullOrEmpty(err))
                TempData["alert"] = "success#操作成功";
            else
                TempData["alert"] = "error#" + err;

            return RedirectToAction("index");
        }

        public ActionResult Edit(int Id)
        {
            ViewBag.Menus = MenuService.GetMenuAll();
            Role role = RoleService.Get(Id);
            if (role == null)
                return HttpNotFound();
            return View(role);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(RoleModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Menus = MenuService.GetMenuAll().Select(o => o.Id).ToList();
                return View(model);
            }
            if (model.Powers == null)
            {
                TempData["alert"] = "error#请选择所要添加的权限信息";
                return RedirectToAction("index");
            }
            Role role = new Role { Name = model.Name, Id = model.Id, Powers = string.Join(",", model.Powers) };
            string err = RoleService.RoleEdit(role);
            if (string.IsNullOrEmpty(err))
                TempData["alert"] = "success#操作成功";
            else
                TempData["alert"] = "error#" + err;


            return RedirectToAction("index");
        }

        public ActionResult Delete(int id)
        {
            var m = RoleService.Get(id);
            if (m == null)
                return HttpNotFound();

            return View(m);
        }

        [HttpPost]
        [ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult CmdDelete(int id)
        {
            string err = RoleService.RoleRemove(id);
            if (string.IsNullOrEmpty(err))
                TempData["alert"] = "success#删除成功";
            else
                TempData["alert"] = "error#" + err;

            return RedirectToAction("index");
        }

        //// 角色用户授权
        //// GET: Admin/Authorize
        //public ActionResult Authorize(int id)
        //{
        //    return View(RoleService.GetRole(id));
        //}

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Authorize(AuthorizeModel model)
        //{
        //    if (!ModelState.IsValid)
        //        return View(RoleService.GetRole(model.Rid));

        //    if (RoleService.Authorize(model.Rid, new Staff { Name = model.Name, Email = model.Email, Status = 0 }) == 0)
        //        TempData["alert"] = "error#操作失败，已存在";
        //    else
        //        TempData["alert"] = "success#操作成功";

        //    return RedirectToAction("index", "staff", new { name = model.Name });
        //}

    }
}